require(['loading'], function(loading) {
	var page;
	var headPicUrl;
	window.onpopstate = function(event) {
		if(page == 2){
			history.back();
		}else{
			$('#modifyName,#modifyPosition,#modifyMobile').hide();
			$('#list').show();
		}
	};
    //应用实例化
    var personalDetail = new Vue({
        el: '#personalDetail',
        data: {
            detail: {},
            showLoading: false,
            apiUrl: wxUrl + '/v3.3/personalInfo/info',
            modifyUrl: wxUrl + '/v3.3/personalInfo/postPersonalInfo',
            validCode: true,
            yzmUrl: wxUrl + '/v3.3/personalInfo/sendVerifyCode',
            changeUrl: wxUrl + '/v3.3/personalInfo/modifyMobile',
            yzmitem: {
                params:{
                    token: token,
                    mobile: '',
                    type:3
                }
            },
            changeitem: {
                params:{
                    token: token,
                    userToken:userToken,
                    newMobile: '',
                    verifyCode:''
                }
            },
            item: {
                params:{
                    token: token,
                    userToken: userToken
                }
            },
            modifyItem: {
                params:{
                    token: token,
                    userToken: userToken,
                    headPic: headPicUrl,
                    nickName: '',
                    manifesto: ''
                }
            }
        },
        ready: function() {
            this.getPersonalDetailData();
            bindMobileKeyUp();
            bindYzmClick();
            bindSubmit();
        },
        methods: {
            getPersonalDetailData: function() {
                this.$http.get(this.apiUrl, this.item)
                    .then(function(response) {
                        var result = JSON.parse(response.body);
                        var detailData = result.data;
                        console.log(detailData);
                        this.$set('detail', detailData);
                        headPicUrl = detailData.headPicUrl;
                    },function(response) {
                        //errorCallback
                    });
            },
            clickModify: function (type) {
            	history.pushState({},"","");
            	page = 1;
            	$('#list').hide();
            	if(type == 1){
            		$('#modifyName').show(); //昵称
            	} else if(type == 3){
            		$('#modifyPosition').show(); // 签名
            	}  else if(type == 2){
            		$('#modifyMobile').show();  //手机号
            	}
            },
            modifySubmit: function (e, type) {
            	if (type == 1) {
            		var nickName = $('#userName').val();
            		if(nickName == null || nickName == undefined || nickName == "" || nickName.length<2){
            			promptBox('昵称必须大于两个字！');
				        return;
				    }else{
				    	var reg = /^[a-zA-z0-9\u4E00-\u9FA5]*$/;
						if(!reg.test(nickName)){
							promptBox('昵称不能含有特殊字符！');
				        	return;
						}
				    }
            		this.modifyItem.params = {
            			token: token,
	                    userToken: userToken,
	                    headPic: headPicUrl,
	                    nickName: nickName,
	                    manifesto: this.detail.manifesto
					};
					console.log(headPicUrl);
            	} else if (type == 3) {
            		var position = $('#userPosition').val();
            		if(position == null || position == undefined || position == "" || position.length<2){
            			promptBox('签名必须大于两个字！');
				        return;
				    }else{
				    	var reg = /^[a-zA-z0-9\u4E00-\u9FA5]*$/;
						if(!reg.test(position)){
							promptBox('签名不能含有特殊字符！');
				        	return;
						}
				    }
            		this.modifyItem.params = {
            			token: token,
	                    userToken: userToken,
	                    headPic: headPicUrl,
	                    nickName: this.detail.nickName,
	                    manifesto: position
	                };
	            }else if (type == 6) {
            		this.modifyItem.params = {
            			token: token,
	                    userToken: userToken,
	                    headPic: this.detail.headPic,
	                    nickName: this.detail.nickName,
	                    manifesto: this.detail.manifesto
	                };
	            }
            	this.$http.get(this.modifyUrl, this.modifyItem)
                    .then(function(response) {
                        var result = JSON.parse(response.body);
                        if(result.code == 0){
                        	if(result.data.success){
                        		page = 2;
                        		this.getPersonalDetailData();
                        		$("#modifyName,#modifyPosition").hide();
                        		$('#list').show();
                        	}else{
                        		promptBox("修改失败，请重试！");
                        	}
                        }
                    },function(response) {
                        //errorCallback
                    });
            },
			getyzm: function() {
                if(!checkPhone()){
                    return;
                }
                var phone = $("#tel").val();
                var code=$(".msgs");
                personalDetail.yzmitem.params.mobile=phone;
                var time=60;
                if (this.validCode) {
                    this.validCode=false;
                    code.addClass("msgs1");
                    this.$http.get(this.yzmUrl, this.yzmitem)
                    .then(function(response) {
                        var result = JSON.parse(response.body);
                        if(result.data!=null){
                            var success = result.data.success;
                            console.log(success)
                            if(success){
                                var t=setInterval(function  () {
                                    time--;
                                    code.html(time+"s后重发");
                                    if (time==0) {
                                        clearInterval(t);
                                        code.html("重发验证码")
                                        code.css("color","#FF9500");
                                        this.validCode=true;
                                        code.removeClass("msgs1");
                                    }
                                },1000)
                              
                            }else{
                                this.validCode=true;
                                code.removeClass("msgs1"); 
                            }
                        }else{
                            this.validCode=true;
                            code.removeClass("msgs1"); 
                        }
                    },function(response) {
                        //errorCallback
                    });
                }
            },
            toChangeMobile: function() {
                this.$http.get(this.changeUrl, this.changeitem)
                    .then(function(response) {
                        var result = JSON.parse(response.body);
                        if(result.code != null && result.code == 0){
                            if(result.data!=null){
                                var success = result.data.success;
                                if(success){
                                    promptBox("更改手机号成功");
                                   personalDetail.getPersonalDetailData();
                                   location.reload();
                                    setTimeout(function(){
                                    	 $("#modifyMobile").hide();
                                    	 	$('#list').show();
                                    },1000);
                                }else{
                                    promptBox("更改手机号失败，请重试");
                                }
                            }
                        }
                        
                    },function(response) {
                        //errorCallback
                    });
            }
        }
    });
	//上传头像
	$('#headPic').diyUpload({
		url: wxUrl + '/v3.3/personalInfo/upload',
		multiple: false,
		success:function(data){
			if(data.data.success){
				promptBox('上传成功！');
				personalDetail.detail.headPic = data.data.url;
				personalDetail.modifySubmit(this,6);
			} else{
				promptBox('上传失败！');
			}
		},
		error:function(err){
			promptBox(err);
		}
	});
	function bindMobileKeyUp(){
        $('.content').on('keyup',function(){
            $('.clear').show();
        });
        $('.clear').click(function(){
            $(this).hide();
            $('.content').val('');
        });
    }

    function bindYzmClick(){
        //获取短信验证码
        var validCode=true;
        $(".msgs").click(function() {
            personalDetail.getyzm();
        })
    }

    function checkPhone(){
        var phone = $("#tel").val();
        if(phone == "") {
            promptBox("手机号码不能为空");
        } else {
            var re = /^[1][34587]\d{9}$/; //手机号码验证正则表达式
            if(re.test(phone)) {
                return true;
            } else {
                promptBox("请输入正确的手机号码");
            }
        }
        return false;
    }


    function bindSubmit(){
        $("#submit").click(function() {
            if(checkPhone()){
                var yzm = $("#yz").val();
                if(yzm == ''){
                    promptBox("请输入验证码");
                    return;
                }
                personalDetail.changeitem.params.verifyCode=yzm;
                var phone = $("#tel").val();
                personalDetail.changeitem.params.newMobile=phone;
                personalDetail.toChangeMobile();
                personalDetail.getPersonalDetailData();
            }
        });
    }
});
